package com.hg.library.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

/**
 * Created by admin on 2017/3/3.
 */

public class NewCreateButton extends Button {

    private static final String TAG = "NewCreateButton";

    private float padding = 10;
    private boolean isStart;

    private Paint mPaint = new Paint();

    private float degree = padding / 2;
    private float d = degree * 2;
    private float drawP1degree, drawP2degree = degree;

    public NewCreateButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        setBackground(null);

        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                click();
            }
        });
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(3);
        mPaint.setColor(Color.argb(255, 65, 72, 75));

        canvas.drawRect(padding / 2, padding * 3 / 2f, getWidth() - padding / 2, getHeight() - padding / 2, mPaint);

        if (isStart) {

            if (!drawP1(canvas)) ;
            else if (!drawP2(canvas)) ;
            invalidate();
        } else {
            canvas.drawLine(padding / 2 + degree, padding * 3 / 2f - degree, getWidth() - padding / 2 - degree, padding * 3 / 2f - degree, mPaint);
            canvas.drawLine(padding / 2 + degree * 2, padding * 3 / 2f - degree * 2, getWidth() - padding / 2 - degree * 2, padding * 3 / 2f - degree * 2, mPaint);
        }

    }

    private boolean drawP1(Canvas canvas) {

        canvas.drawLine(padding / 2 + drawP1degree, padding * 3 / 2f - drawP1degree, getWidth() - padding / 2 - drawP1degree, padding * 3 / 2f - drawP1degree, mPaint);

        drawP1degree += 0.2f;

        if (drawP1degree >= degree) {
            drawP1degree = degree;
            return true;
        }

        return false;
    }

    private boolean drawP2(Canvas canvas) {

        canvas.drawLine(padding / 2 + drawP2degree, padding * 3 / 2f - drawP2degree, getWidth() - padding / 2 - drawP2degree, padding * 3 / 2f - drawP2degree, mPaint);

        drawP2degree += 0.2f;

        if (drawP2degree >= degree * 2) {
            drawP2degree = degree * 2;
            finish();
            return true;
        }
        return false;
    }

    public void finish() {
        isStart = false;

        drawP1degree = 0;
        drawP2degree = degree;
    }

    public void click() {
        if (!isStart) {
            isStart = true;
            invalidate();
        }
    }
}
